package com.sks.web.action.house;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.stereotype.Controller;

import com.sks.bean.QueryResult;
import com.sks.bean.pojo.HouseType;
import com.sks.service.house.HouseTypeService;
import com.sks.utils.SiteUrl;
import com.sks.utils.StringUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.HouseTypeForm;

@Controller("/cms/house/type")
public class HouseTypeAction extends BasicAction {
	@Resource(name = "houseTypeServiceBean")
	private HouseTypeService houseTypeService;

	@Permission(module = "info", privilege = "view")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("houseTypeId", "asc");
		QueryResult<HouseType> qr = houseTypeService.getScrollData(-1, -1, orderby);
		request.setAttribute("houseTypeList", qr.getResultlist());
		this.setReadOnly(request, "list");
		return mapping.findForward(LIST);
	}

	/**
	 * 类别添加界面
	 */
	@Permission(module = "info", privilege = "update")
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	   this.saveToken(request);
	   HouseTypeForm formbean = (HouseTypeForm) form;
	   formbean.setMethod(ADD);
		return mapping.findForward(EDIT);
	}
	
	public void beforeadd(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response)
         throws Exception {
	   ActionMessages errors = new ActionMessages();
	   HouseTypeForm formbean = (HouseTypeForm) form;
	   
	   if (StringUtil.isEmpty(formbean.getName())) {
		   errors.add("name", new ActionMessage("errors.required", getMessage("info.houseType.field.name")));
	   }
	   
	   List params = new ArrayList();
		params.add(formbean.getName());
		StringBuffer jpql = new StringBuffer("o.name = ?" + params.size());
		if (EDIT.equals(formbean.getMethod())) {
			params.add(formbean.getHouseTypeId());
			jpql.append(" and o.houseTypeId <> ?" + params.size());
		}
	   QueryResult<HouseType> m = houseTypeService.getScrollData(-1, -1, jpql.toString(),
				params.toArray());
	   if (null != m && m.getResultlist().size() > 0) {
         // 提示信息
         errors.add("name", new ActionMessage("errors.repeat", getMessage("info.houseType.field.name")));
         request.setAttribute(ACTION, formbean.getMethod());
      }
      saveErrors(request, errors);
	}

	/**
	 * 类别添加
	 */
	@Permission(module = "info", privilege = "update")
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeForm formbean = (HouseTypeForm) form;
		HouseType type = new HouseType(formbean.getName());
		houseTypeService.save(type);
		request.setAttribute(MESSAGE, getMessage("display.add") + getMessage("info.houseType") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.type"));
		return mapping.findForward(MESSAGE);
	}

	/**
	 * 类别修改界面
	 */
	@Permission(module = "info", privilege = "view")
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	   this.saveToken(request);
		HouseTypeForm formbean = (HouseTypeForm) form;
		HouseType type = houseTypeService.find(formbean.getHouseTypeId());
		formbean.setName(type.getName());
		formbean.setMethod(EDIT);
		this.setReadOnly(request, "editUI");
		return mapping.findForward(EDIT);
	}
	
	public void beforeedit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeForm formbean = (HouseTypeForm) form;
		this.beforeadd(mapping, formbean, request, response);
	}

	/**
	 * 类别修改
	 */
	@Permission(module = "info", privilege = "update")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeForm formbean = (HouseTypeForm) form;
		HouseType type = houseTypeService.find(formbean.getHouseTypeId());
		type.setName(formbean.getName());
		houseTypeService.update(type);
		request.setAttribute(MESSAGE, getMessage("display.edit") + getMessage("info.houseType") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.type"));
		return mapping.findForward(MESSAGE);
	}

	/**
	 * 类别删除
	 */
	@Permission(module = "info", privilege = "update")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseTypeForm formbean = (HouseTypeForm) form;
		houseTypeService.delete(formbean.getHouseTypeId());
		request.setAttribute(MESSAGE, getMessage("display.delete") + getMessage("info.houseType") + getMessage("display.success"));
		request.setAttribute("urladdress", SiteUrl
				.readUrl("com.sks.web.action.house.type"));
		return mapping.findForward(MESSAGE);
	}
}
